package com.food.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.food.user.dto.UserAddressDto;
import com.food.user.entity.UserAddress;
import com.food.user.exception.userAddressException.UserAddressException;
import com.food.user.exception.userAddressException.UserAddressExceptionCode;
import com.food.user.form.AddUserAddressForm;
import com.food.user.form.UpdateUserAddressForm;
import com.food.user.mapper.UserAddressMapper;
import com.food.user.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.food.user.util.JwtUtil;
import com.food.user.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    @Transactional
    public UserAddress addUserAddress(AddUserAddressForm form) {
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        //拼接条件 要4个属性都相同 才代表地址重复
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address_info",form.getAddressInfo()).eq("receiver_name",form.getReceiverName())
                .eq("receiver_phone",form.getReceiverPhone()).eq("user_id",userId);
        UserAddress userAddress = userAddressMapper.selectOne(queryWrapper);
        if(userAddress!=null) throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_IS_EXIST);
        //赋值
        UserAddress userAddress1 = BeanUtil.toBean(form, UserAddress.class);
        userAddress1.setUserId(Long.valueOf(userId));
        userAddress1.setAddressState(1);
        userAddressMapper.insert(userAddress1);
        return userAddress1;
    }

    @Override
    @Transactional
    public UserAddress updateUserAddress(UpdateUserAddressForm form) throws Exception{

        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);

        UserAddress userAddress = userAddressMapper.selectById(form.getAddressId());
        if(userAddress==null) throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_NOT_EXIST);
        if (userAddress.getUserId()!=userId.longValue()){
            throw new Exception("无权修改");
        }
        userAddress.setAddressInfo(form.getAddressInfo());
        userAddress.setReceiverName(form.getReceiverName());
        userAddress.setReceiverPhone(form.getReceiverPhone());
        userAddressMapper.updateById(userAddress);
        return userAddress;
    }

    @Override
    public void delUserAddress(Long addressId) throws Exception{

        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);


        UserAddress userAddress = userAddressMapper.selectById(addressId);
        if(userAddress==null) throw new UserAddressException(UserAddressExceptionCode.USER_ADDRESS_NOT_EXIST);
        if (userAddress.getUserId()!=userId.longValue()){
            throw new Exception("无权删除");
        }
        userAddressMapper.deleteById(addressId);
    }

    @Override
    public ArrayList<UserAddressDto> listAllAddress() {
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        //获取用户id
        String accessToken = WebUtil.getRequest().getHeader("accessToken");
        Integer userId = JwtUtil.getUserId(accessToken);
        //找到用户所有地址
        queryWrapper.eq("user_id",userId);
        ArrayList<UserAddressDto> list = new ArrayList<>();
        List<UserAddress> userAddresses = userAddressMapper.selectList(queryWrapper);
        for (UserAddress userAddress : userAddresses) {
            UserAddressDto dto = new UserAddressDto();
            dto.setAddressId(userAddress.getAddressId());
            dto.setAddressInfo(userAddress.getAddressInfo());
            dto.setReceiverName(userAddress.getReceiverName());
            dto.setReceiverPhone(userAddress.getReceiverPhone());
            list.add(dto);
        }
        return list;
    }
}
